package com.engc.common.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.bean.DbColumn;
import com.engc.common.bean.DbView;
import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;

/**
 * 数据权限控制
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/sysDataPrivate")
public class SysDataPrivate {
	private BaseService baseService;

	@RequestMapping("/view")
	public String view() {
		return "system/dataprivate/dataprivate";
	}

	@RequestMapping("/insert")
	@ResponseBody
	public Map<String, Object> InserRole(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		record.put("id", baseService.getSeqValue("SEQ_SYS_DATAPRIVATE"));
		record.put("orgid", BaseUtils.getCurrentUser(request).getOrgid());
		try {
			baseService.Insert("SYS_AUTHORITY", record);
			return BaseUtils.getSuccessMessage("新增成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("新增失败");
		}
	}

	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> UpdateRole(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getUpdateParam(request, "id");
		record.put("orgid", BaseUtils.getCurrentUser(request).getOrgid());
		try {
			baseService.Update("SYS_AUTHORITY", record, "id=?", request.getParameter("id"));
			return BaseUtils.getSuccessMessage("修改成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("修改失败!");
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> DelRole(@RequestParam("id") String id) {
		try {
			baseService.Delete("SYS_AUTHORITY", "id=?", id);
			return BaseUtils.getSuccessMessage("删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("删除失败!");
		}
	}

	/**
	 * 查询获取单条数据权限记录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDataPrivate")
	@ResponseBody
	public Map<String, Object> getDataPrivate(HttpServletRequest request) {
		String id = request.getParameter("id");
		Map<String, Object> record = baseService.Read("select * from sys_dataprivate where id = ?", id);
		return BaseUtils.getSuccessMessage(record);
	}

	@RequestMapping("/getDataViews")
	@ResponseBody
	public List<DbView> getDataViews() {
		List<DbView> dbviews = new ArrayList<DbView>();
		try {
			String basepath = this.getClass().getResource("/config/dataprivate").getPath();
			File dataprivate = new File(basepath);
			if (dataprivate.isDirectory()) {
				File[] datasettings = dataprivate.listFiles();
				if (datasettings.length > 0) {
					for (int i = 0; i < datasettings.length; i++) {
						File file = datasettings[i];
						SAXReader saxReader = new SAXReader();

						Document doc = saxReader.read(file);
						Element root = doc.getRootElement();
						if ("dbsettings".equals(root.getName())) {
							List<Element> views = root.elements("view");
							for (Element view : views) {
								DbView dbview = new DbView();
								dbview.setName(view.attributeValue("name"));
								dbview.setDisplay(view.attributeValue("display"));
								List<Element> columns = view.elements("column");
								for (Element column : columns) {
									DbColumn dbcolumn = new DbColumn();
									dbcolumn.setName(column.attributeValue("name"));
									dbcolumn.setDisplay(column.attributeValue("display"));
									dbcolumn.setType(column.attributeValue("type"));
									dbview.addColumns(dbcolumn);
								}
								dbviews.add(dbview);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dbviews;
	}

	@Resource
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
